/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.steps;

import junit.framework.Assert;
import junit.framework.AssertionFailedError;

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;

import td.pages.AbstractPage;
import td.pages.Pages;
import td.shared.Constants.Control;
import td.shared.Constants.LOGIN;
import td.shared.Constants.MAIN;

/**
 * @author Artem Shamsutdinov
 * 
 */
public class WebSteps {

	private final Pages pages;

	private AbstractPage currentPage;

	public WebSteps(Pages pages) {
		this.pages = pages;
	}

	// pages.home().open();
	// pages.findSteps().open();
	// pages.findSteps().pageIsShown();
	// pages.findSteps().find(step);
	// pages.findSteps().found(stepsOrMethods);

	@Given("The datasore exists")
	public void findLocalGAEStore() {

	}

	@Given("the ThoughtDialog home PAGE \"$pageUrl\" IS PRESENT")
	public void thoughtDialogHomePage(String pageUrl) {
		pages.home().open(pageUrl);
	}

	private AbstractPage getPage(String description) {
		if (MAIN.NAME.equals(description)) {
			return pages.home();
		} else if (LOGIN.NAME.equals(description)) {
			return pages.loginJoin();
		}
		throw new AssertionFailedError("Invalid Page description '" + description
				+ "'");
	}

	@When("I click the $controlType \"$controlText\" on \"$pageName\" page")
	public void clickControl(String controlType, String controlText,
			String pageName) {
		this.currentPage = getPage(pageName);
		clickControl(controlType, controlText);
	}

	@When("I click the $controlType \"$controlText\"")
	public void clickControl(String controlType, String controlText) {
		if (this.currentPage == null) {
			Assert.fail("currentPage is not known.");
		}
		if (Control.BUTTON.equals(controlType)) {
			currentPage.clickButtonByText(controlText);
		} else if (Control.CHECK_BOX.equals(controlType)) {
			currentPage.clickByLabel(controlText);
		} else {
			Assert.fail("Invalid control type '" + controlType
					+ "' for action 'click'");
		}
	}

	@Then("the $controlType \"$controlText\" is present on \"$pageName\" page")
	public void verifyIsPresent(String controlType, String controlText,
			String pageName) {
		this.currentPage = getPage(pageName);
		verifyIsPresent(controlType, controlText);
	}

	@Then("the $controlType \"$controlText\" is present")
	public void verifyIsPresent(String controlType, String controlText) {
		if (this.currentPage == null) {
			Assert.fail("currentPage is not known.");
		}
		if (Control.BUTTON.equals(controlType)) {
			currentPage.foundByButtonText(controlText);
		} else if (Control.CHECK_BOX.equals(controlType)) {
			currentPage.foundByLabelText(controlText);
		} else if (Control.TEXT_BOX.equals(controlType)) {
			currentPage.foundByLabelText(controlText);
		} else if (Control.TEXT.equals(controlType)) {
			currentPage.found(controlText);
		} else {
			Assert.fail("Invalid control type '" + controlType
					+ "' for action 'is present'");
		}
	}

	@When("I type \"$text\" in $controlType \"$controlText\" on \"$pageName\" page")
	public void type(String text, String controlType, String controlText,
			final String pageName) {
		this.currentPage = getPage(pageName);
		type(text, controlType, controlText);
	}

	@When("I type \"$text\" in $controlType \"$controlText\"")
	public void type(String text, String controlType, String controlText) {
		if (this.currentPage == null) {
			Assert.fail("currentPage is not known.");
		}
		if (Control.TEXT_BOX.equals(controlType)) {
			currentPage.type(controlText, text);
		} else {
			Assert.fail("Invalid control type '" + controlType
					+ "' for action 'type'");
		}
	}

	@Given("the \"Login\" page is up at \"$pageUrl\"")
	public void loginPageIsUp(String pageUrl) {
		thoughtDialogHomePage(pageUrl);
		clickControl(Control.BUTTON, MAIN.TEXT.BTN_LOGIN_JOIN, MAIN.NAME);
		verifyIsPresent(Control.TEXT_BOX, LOGIN.TEXT.TXT_THINKER_ID, LOGIN.NAME);
		verifyIsPresent(Control.TEXT_BOX, LOGIN.TEXT.TXT_PASSWORD);
		verifyIsPresent(Control.CHECK_BOX, LOGIN.TEXT.CHK_JOIN);
		verifyIsPresent(Control.BUTTON, LOGIN.TEXT.BTN_LOGIN);
	}

	@Given("the \"Login\" page is up in Join mode at \"$pageUrl\"")
	public void loginPageIsUpInJoinMode(String pageUrl) {
		loginPageIsUp(pageUrl);
		clickControl(Control.CHECK_BOX, LOGIN.TEXT.CHK_JOIN);
		verifyIsPresent(Control.BUTTON, LOGIN.TEXT.BTN_JOIN);
		verifyIsPresent(Control.TEXT_BOX, LOGIN.TEXT.TXT_EMAIL);
	}
}
